<template>
    <div class="main-container">
        <navbar title="购物车"></navbar>
        <div class="cartContainer">

            <div class="promptMsg" v-if="!cartGoodsList.length">
                <div>
                    <span class="mui-icon mui-icon-extra mui-icon-extra-cart"></span>
                    购物车是空的，
                    <router-link to="/goods">去买买买？</router-link>
                </div>
            </div>
            <template v-else>
                <div class="mui-card settlementList">
                    <div class="mui-input-group">
                        <div class="mui-input-row mui-checkbox mui-left cartHeader">
                            <label>
                                Convenience Store
                                <input name="checkbox" type="checkbox" :checked="isSelectAll?'checked':''" @click="selectAll">
                                <span>已免运费</span>
                            </label>
                        </div>
                        <!-- 从vuex中拿到数据 -->
                        <div v-for="(item,i) in cartGoodsList" :key="i">
                            <div class="discount">
                                <span class="mui-badge mui-badge-danger">满减</span>已购买一件，可享受换购优惠</div>
                            <div class="mui-input-row mui-checkbox mui-left goodItem">
                                <input name="checkbox" type="checkbox" :value="item.id" v-model="checkboxModel">
                                <div class="goodItem-info">
                                    <img :src="item.imgsrc" alt="">
                                    <div class="info">
                                        <router-link :to="'/goods/'+item.id" tag="h1">{{item.title}}</router-link>
                                        <span class="fushu">3.47kg i7 16G+512G</span>
                                        <div class="price" id="pricebox">
                                            <span>￥ {{item.price * item.count | priceFormat}}</span>
                                            <!-- 购物车的加减框  传递数量和ID -->
                                            <numberBox_cart :buycount="item.count" :id="item.id"></numberBox_cart>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
                <div class="mui-card" id="settlementbox">
                    <div class="mui-input-group">
                        <div class="mui-input-row mui-checkbox mui-left">
                            <label>
                                <span>全选</span>
                                <input name="checkbox" type="checkbox" :checked="isSelectAll?'checked':''" @click="selectAll">
                            </label>
                            <span class="total">合计：￥
                                <span>{{this.$store.getters.getTotalPrice | priceFormat}}</span>
                                <!-- v-html中不能使用过滤器？ -->
                            </span>
                            <mt-button type="danger" id="settlement" @click="gotoSettlement">去结算({{this.$store.getters.getTotalQuantity}})</mt-button>
                        </div>
                    </div>
                </div>
            </template>
        </div>
    </div>

</template>

<script>
    import numberBox_cart from '../../components/common/numberBox_cart.vue'
    import NavBar from '../common/navbar.vue'
    //加载提示框
    import { Indicator,Toast } from 'mint-ui';

    export default {
        data() {
            return {
                //全选
                isSelectAll: false,
                //保存复选框选择的值
                checkboxModel: [],
                //从vuex中拿到数据
                cartGoodsList: this.$store.state.AllGoodsInfo
            }
        },
        created() {
            //判断购物车中的商品, enable =true 的商品默认被勾选
            this.cartGoodsList.forEach(item => {
                if (item.enable) {
                    //console.info(item);
                    this.checkboxModel.push(item.id);
                }
            });
            this.selectAllButton()
        },
        watch: {
            checkboxModel: function () {
                this.selectAllButton();
                console.log(this.checkboxModel)
                console.log('复选框有变化，将 enable 的变化 保存到 vuex')
                this.$store.commit('updateEnable', this.checkboxModel)
            }
        },
        methods: {
            //全选反选
            selectAll: function () {
                this.checkboxModel = []; //先清空
                this.isSelectAll = !this.isSelectAll;
                if (this.isSelectAll) {
                    this.cartGoodsList.forEach(item => {
                        this.checkboxModel.push(item.id);
                    })
                } else {
                    this.checkboxModel = [];
                }
            },
            //如果选中了购物车所有的商品，全选按钮将被选中。
            selectAllButton: function () {

                // if (this.checkboxModel.length == 0) {

                //     return;
                // }
                if (this.cartGoodsList.length == this.checkboxModel.length) {
                    this.isSelectAll = true;
                } else {
                    this.isSelectAll = false;
                }
            },
            //模拟结算
            gotoSettlement() {

                if(!this.checkboxModel.length)
                {
                    Toast({
                        message: '请选择商品！'
                    });
                    return;
                }
                Indicator.open({
                    text: '支付环境检测...',
                    spinnerType: 'fading-circle'
                });

                setTimeout(function () {
                    Indicator.close();
                }, 1500)
            }
        },
        components: {
            numberBox_cart,
            navbar: NavBar
        }
    }
</script>


<style lang="scss" scoped>
    .main-container {
        height: 100%;
    }

    .cartContainer {
        height: 100%;
        .promptMsg {

            display: flex;
            justify-content: center;
            align-items: center;
            height: 100%;

            div {

                height: 150px; // position: absolute;
                // left: 0;
                // right: 0;
                // top: 0;
                // bottom: 0;
                // margin: auto;
                text-align: center;
                color: #999;
                font-size: 18px;

                .mui-icon {
                    font-size: 100px;
                    display: block;
                    color: #999;
                }
            }
        }
        .settlementList {
            margin-bottom: 60px !important;
        }

        .discount {
            padding-left: 10px;
            span {
                margin-right: 10px;
            }
        }
        .cartHeader {
            margin-bottom: 20px;
            label {
                display: flex;
                justify-content: space-between
            }
        }
        .mui-checkbox.mui-left input[type=checkbox] {
            left: 10px;
            margin-right: 8px;
        }
        .mui-card {
            margin: 10px 0;
            box-shadow: initial;

            .goodItem {
                display: flex;
                align-items: center;
                height: 110px;
                padding: 0 10px;

                .mui-input-group .mui-input-row:after {
                    display: none;
                }
                .goodItem-info {
                    display: flex;
                    align-items: center;
                    box-shadow: 0 1px 5px rgba(0, 0, 0, .3);
                    width: 100%;

                    .info {
                        width: 100%;
                    }
                }

                input {
                    position: static;
                }
                img {
                    width: 80px;
                    height: 80px;
                    margin: 0 10px;
                }
                h1 {
                    font-size: 15px;
                    font-weight: normal;
                    line-height: 1.3;
                    text-overflow: ellipsis;
                    overflow: hidden;
                    height: 40px;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    margin: 0;
                    padding: 5px 4px 0;
                }
                .fushu {
                    color: #333;
                    font-size: 12px;
                }
                .price {
                    color: red;
                    font-weight: bold;
                    font-size: 16px;
                    padding-top: 5px;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;

                    .mui-numbox {
                        width: 80px;
                        height: 25px;
                        padding: 0 25px;
                        border: 0;
                        background-color: transparent;
                    }
                }
            }
        }

        #settlementbox {
            position: fixed;
            left: 0;
            bottom: 50px;
            width: 100%;
            margin: 0;

            .mui-checkbox {
                display: flex;
                justify-content: space-between;
                align-items: center;
                background: #fff;
                height: 40px;

                label {
                    width: auto;
                }


                button {
                    // height: 100%;
                    // border-radius: 0;
                    // border: 0;
                    // background-color: red;
                    // color: #fff;
                    // font-size: 16px;
                }
            }

            .total {
                font-size: 18px;
                span {
                    font-size: 20px;
                }
            }
        }
    }
</style>

<style lang="scss">
    #pricebox {
        button {
            width: 25px;
        }

        .mui-input-numbox {
            border: 0 !important;
            background-color: #eee;
            color: #333;
            font-size: 14px;
        }

    }

    #settlement {

        height: 100%;
        border-radius: 0;
        label {
            padding: 0 !important;
        }



    }
</style>